package com.njtoyo.taxi.admin.service.platform.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.map.MapUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.njtoyo.taxi.admin.mapper.secondary.entity.Versions;
import com.njtoyo.taxi.admin.mapper.secondary.enums.VersionModel;
import com.njtoyo.taxi.admin.mapper.secondary.enums.VersionUserGroup;
import com.njtoyo.taxi.admin.mapper.secondary.mapper.VersionMapper;
import com.njtoyo.taxi.admin.rest.wrapper.platform.appVersion.AppVersionWrapper;
import com.njtoyo.taxi.admin.service.platform.VersionService;
import com.njtoyo.taxi.admin.utils.OssClient;
import com.taxi.entity.common.RestResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class VersionServiceImpl implements VersionService {
    @Autowired
    private VersionMapper appVersionMapper;

    public RestResult list(Integer current, Integer size, String title, VersionModel appType, VersionUserGroup userGroup) {
        Page<Versions> page = new Page<>(current, size);
        Page<Versions> appVersionPage = appVersionMapper.selectPage(page, Wrappers.<Versions>lambdaQuery()
                .like(StrUtil.isNotBlank(title), Versions::getTitle, title)
                .eq(ObjectUtil.isNotNull(appType), Versions::getModel, appType)
                .eq(ObjectUtil.isNotNull(userGroup), Versions::getUseGroup, userGroup)
                .orderByDesc(Versions::getId));
        return RestResult.success(appVersionPage);
    }

    @Override
    public RestResult uploadApp(MultipartFile file) {
        String url = "";
        try {
            url = OssClient.uploadFile(file.getInputStream(), file.getOriginalFilename(), OssClient.OOS_BUCKET_NAME_APP);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
        return RestResult.success(url);
    }

    @Override
    public RestResult add(AppVersionWrapper appVersionWrapper) {
        List<String> logs = Arrays.stream(appVersionWrapper.getContentArray()).
                filter(x -> StrUtil.isNotBlank(MapUtil.getStr(Convert.toMap(String.class, Object.class, x), "text")))
                .map(x -> MapUtil.getStr(Convert.toMap(String.class, Object.class, x), "text")).collect(Collectors.toList());
        String contentText = StrUtil.join("|", logs);
        Versions versions = BeanUtil.copyProperties(appVersionWrapper, Versions.class);
        versions.setContent(contentText);
        appVersionMapper.insert(versions);
        return RestResult.success();
    }

    @Override
    public RestResult getVersionInfo(Long id) {
        Versions versions = appVersionMapper.selectById(id);
        if (ObjectUtil.isNull(versions)) {
            return RestResult.failed("版本不存在");
        }
        String[] split = StrUtil.split(versions.getContent(), "|");
        AppVersionWrapper appVersionWrapper = BeanUtil.copyProperties(versions, AppVersionWrapper.class);
        appVersionWrapper.setContentArray(Arrays.stream(split).map(x -> new AppVersionWrapper.UpdateLog(x)).toArray(AppVersionWrapper.UpdateLog[]::new));
        return RestResult.success(appVersionWrapper);
    }

    @Override
    public RestResult updateById(AppVersionWrapper appVersionWrapper) {
        List<String> logs = Arrays.stream(appVersionWrapper.getContentArray()).
                filter(x -> StrUtil.isNotBlank(MapUtil.getStr(Convert.toMap(String.class, Object.class, x), "text")))
                .map(x -> MapUtil.getStr(Convert.toMap(String.class, Object.class, x), "text")).collect(Collectors.toList());
        String contentText = StrUtil.join("|", logs);
        Versions versions = BeanUtil.copyProperties(appVersionWrapper, Versions.class);
        versions.setContent(contentText);
        appVersionMapper.updateById(versions);
        return RestResult.success();
    }
}
